﻿//====================================================================================================================
//Copyright (c) 2011 IdeaBlade
//====================================================================================================================
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//====================================================================================================================
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of 
//the Software.
//====================================================================================================================
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
//OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
//====================================================================================================================

using System.Collections.Generic;
using System.ComponentModel.Composition;
using Caliburn.Micro;
using Caliburn.Micro.Extensions;
using HelloWorld.Events;
using IdeaBlade.Application.Framework.Core.Composition;
using IdeaBlade.EntityModel;


namespace HelloWorld
{
    [Export(typeof(IMessageProcessor))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class EditOrderMessageProcessor : IMessageProcessor, IHandle<EditOrderMessage>
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly ICompositionCatalogService _catalogService;

        [ImportingConstructor]
        public EditOrderMessageProcessor(IEventAggregator eventAggregator, ICompositionCatalogService catalogService)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);

            _catalogService = catalogService;
        }

        #region IHandle<EditOrderMessage> Members

        public void Handle(EditOrderMessage message)
        {
            IdeaBlade.EntityModel.Coroutine.Start(() => ShowOrderDetail(message));
        }

        /// <summary>
        /// OrderEditorFactory is not available until the xap file gets loaded. AllowDefault=true,
        /// makes sure we don't fail if MEF can't find the OrderEditor. 
        /// AllowRecomposition=true, will tell MEF to set the OrderEditorFactory as
        /// soon as it is available after loading the xap.
        /// </summary>
        [Import("OrderEditor", AllowRecomposition = true, AllowDefault = true)]
        public ExportFactory<object> OrderEditorFactory { get; set; }

        private IEnumerable<INotifyCompleted> ShowOrderDetail(EditOrderMessage message)
        {
            // Let's make sure the xap that contains the OrderEditor is loaded. 
            // Only the first call will load the xap, subsequent calls don't do antyhing.
            yield return _catalogService.AddXap("HelloWorld.Module.xap");

            // Now we can access the OrderEditorFactory. MEF recomposed this instance
            // and provided us with an OrderEditorFactory. 
            var editor = OrderEditorFactory.CreateExport().Value;
            _eventAggregator.Forward(editor, message);
        }

        #endregion
    }
}